package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: StudentServiceImpl
 * @Author: HH
 * @Date: 2025/4/19 18:05
 **/

/**
 * 学员管理ServiceImpl
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页条件查询学员信息
     *
     * @param param
     * @return
     */
    @Override
    public PageBean page(StudentQueryParam param) {
        // 1. 设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());
        // 2. 执行查询
        List<Student> studentList = studentMapper.list(param);

        // 3. 解析封装分页结果
        Page<Student> p = (Page<Student>) studentList;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 添加学员
     *
     * @param student
     */
    @Override
    public void insert(Student student) {
        // 设置创建时间与更新时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getInfo(id);
    }

    /**
     * 修改学员信息
     *
     * @param student
     */
    @Override
    public void update(Student student) {
        // 设置更新时间
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 批量删除学员信息
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    /**
     * 违纪处理
     *
     * @param id
     * @param score
     */
    @Override
    public void handle(Integer id, Integer score) {
        studentMapper.handle(id, score);
    }
}
